<template>
  <view class="shop">
    <view class="desc">
      <view class="title">{{ supplier.name }}</view>
      <view class="text">
        <view>商品数: {{0}}</view>
        <view>关注数: {{0}}</view>
      </view>
      <view class="btn">+关注</view>
    </view>
    <template v-if="active === 0">
      <view class="main">
        <view class="menu">
          <view class="item" v-for="(item, index) in menu" :key="index" :class="{'active' : index === currentIndex}" @click="handleMenu(item, index)">
            <view class="icon"></view>
            <view class="text">{{item.name}}</view>
          </view>
        </view>
        <view class="right">
          <view class="o" v-for="(item, index) in menuChildren" :key="index" @click="goCategory(item)">
            <u--image
                v-if="item.image"
                width="150rpx"
                height="150rpx"
                mode="widthFix"
                :src="baseUrl + item.image.thumbnail_path" />
            <view class="text">{{item.name}}</view>
          </view>
        </view>
      </view>
    </template>
    <template v-else>
      <view class="search">
        <u-search
            placeholder="请输入店铺关键字"
            v-model="keyword"
            height="60rpx"
            searchIconSize="50rpx"
            @search="onSearch"
            @custom="onSearch"
            @clear="onClear"
        ></u-search>
      </view>
      <view class="product">
        <view class="item" v-for="(item, index) in productList" :key="index" @click="handleProduct(item)">
          <u--image
              v-if="item.mainImage"
              width="240rpx"
              mode="widthFix"
              :src="baseUrl + item.mainImage.thumbnail_path" />
          <view class="text">{{item.title}}</view>
        </view>
      </view>
    </template>
    <view class="footer">
      <view class="item" :class="{'active': active === 0}" @click="handleTab(0)">
        <u--image
            width="40rpx"
            height="40rpx"
            mode="widthFix"
            :src="tab2" />
        <view class="text">分类</view>
      </view>
      <view class="item" :class="{'active': active === 1}" @click="handleTab(1)">
        <u--image
            width="40rpx"
            height="40rpx"
            mode="widthFix"
            :src="tab1" />
        <view class="text">商品</view>
      </view>
    </view>
    <arrow />
    <u-back-top bottom="110" right="40" :scroll-top="scrollTop" icon="arrow-up" :customStyle="{'opacity': '0.88'}"></u-back-top>
  </view>
</template>

<script>

/* 这是商家的主页面 */

import Arrow from '../../components/arrow'

import base64 from '../../util/base64'
import Supplier from '../../api/supplier'
import config from '../../config/config'

const model = new Supplier()

export default {
  components: {
    Arrow,
  },
  onPageScroll(e) {
    this.scrollTop = e.scrollTop;
  },

  onLoad(options) {
    // console.log(options)
    this.id = options.id
    this.fetch()
  },

  computed: {
    tab1() {
      return this.active === 1 ? '/static/shop/product-active.png' : '/static/shop/product.png'
    },
    tab2() {
      return this.active === 0 ? '/static/shop/category-active.png' : '/static/shop/category.png'
    }
  },

  data() {
    return {
      baseUrl: config.baseUrl,
      scrollTop: 0,
      id: 0,
      active: 0,
      supplier: {},
      menu: [],
      menuChildren: [],
      currentIndex: 0,
      productList: [],
      queryList: {
        page: 0,
        pageSize: 50,
        busy: false
      },
      keyword: ''
    }
  },
  onReachBottom() {
    if (this.active === 1) {
      this.getList(true)
    }
  },
  methods: {
    fetch() {
      // 获取分类与店铺信息
      model.getView({ merchant_id: this.id }).then(response => {
        let { data } = response
        data = base64.parse(data)
        // console.log(data)
        this.supplier = data.supplier
        this.menu = [...data.category]
        if (data.category.length > 0) {
          this._onMenu(data.category[0].id)
        }
      })
    },
    // 点击菜单
    handleMenu(item, index) {
      this.currentIndex = index
      this._onMenu(item.id)
    },
    _onMenu(id) {
      model.menu({ parent_id: id, merchant_id: this.id }).then(response => {
        let { data } = response
        data = base64.parse(data)
        console.log(data)
        this.menuChildren = [...data.list]
      })
    },
    // 到分类产品
    goCategory(item) {
      uni.navigateTo({
        url: `/sub_product/category/index?id=${item.id}&supplier=${this.id}`
      })
    },
    handleTab(index) {
      this.active = index
      if (index === 1) {
        this.getList()
      }
    },
    // 全部商品
    getList(append = false) {
      if (this.queryList.busy === false) {
        this.queryList.page++
        // this.loading = true
        this.queryList.busy = true
        model.product({
          ...this.queryList,
          key: this.keyword,
          merchant_id: this.id
        }).then(response => {
          let { data } = response
          data = base64.parse(data)
          if (data.list.length === 0) {
            this.queryList.busy = true
          } else {
            this.queryList.busy = false
            if (append) {
              this.productList = this.productList.concat(data.list)
            } else {
              this.productList = data.list
            }
          }
          // this.total = data.quantity
          console.log(this.productList)
        })
      }
    },
    // 到产品详情
    handleProduct(item) {
      uni.navigateTo({
        url: `/sub_product/detail/index?id=${item.id}`
      })
    },
    onSearch() {
      console.log(this.keyword)
      this.queryList = {
        page: 0,
        pageSize: 50,
        busy: false
      },
      this.getList()
    },
    onClear() {
      this.keyword = ''
      this.queryList = {
        page: 0,
        pageSize: 50,
        busy: false
      },
      console.log('clear')
      this.getList()
    }
  }

}
</script>

<style scoped lang="scss">
.shop {
}
.footer {
  height: 100rpx;
  //background: red;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  border-top: 1rpx solid #f4f6f8;
  display: flex;
  align-items: center;
  justify-content: space-around;
  z-index: 100;
  background: white;
  .item {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    &.active {
      color: red;
    }
  }
}
.desc {
  padding: 10px;
  background: #f9f9f9;
  font-size: 24rpx;
  position: relative;
  .title {
    color: #000;
    margin-bottom: 5px;
  }
  .text {
    color: #999;
    display: flex;
    font-size: 20rpx;
    view {
      margin-right: 20px;
    }
  }
  .btn {
    position: absolute;
    padding: 2px 10px;
    background: red;
    right: 10px;
    top: 10px;
    color: white;
    border-radius: 20px;
  }
}
.main {
  //padding-bottom: 102rpx;
  //display: flex;
  .menu {
    width: 200rpx;
    background: #efeff4;
    //padding: 0 20rpx;
    box-sizing: border-box;
    position: fixed;
    left: 0;
    bottom: 102rpx;
    top: 125rpx;
    /* #ifdef H5 */
    top: 210rpx;
    /* #endif */
    overflow-y: auto;
    .item {
      min-height: 60rpx;
      padding: 20rpx 20rpx 20rpx 0;
      display: flex;
      align-items: center;
      .icon {
        width: 8rpx;
        height: 35rpx;
        margin-right: 20rpx;
      }
      &.active {
        background: white;
        .icon {
          background: red;
        }
        .text {
          color: red;
        }
      }
    }
  }
  .right {
    position: fixed;
    overflow-y: auto;
    //background: red;
    left: 201rpx;
    right: 0;
    top: 125rpx;
    /* #ifdef H5 */
    top: 210rpx;
    /* #endif */
    bottom: 102rpx;
    //flex: 1;
    padding: 20rpx;
    box-sizing: border-box;
    display: flex;
    flex-wrap: wrap;
    /* 垂直空白问题 */
    align-content: start;
    .o {
      width: 165rpx;
      //height: 210rpx;
      margin-right: 5rpx;
      display: flex;
      flex-direction: column;
      justify-content: center;
      margin-bottom: 20rpx;
      .text {
        width: 160rpx;
        height: 40rpx;
        line-height: 40rpx;
        //background: red;
        text-align: center;
        overflow: hidden;
        margin: 10rpx 0;
      }
    }
  }
}
/* 全部产品 */
.product {
  padding: 20rpx 20rpx 102rpx 20rpx;
  display: flex;
  flex-wrap: wrap;
  align-content: start;
  justify-content: space-between;
  .item {
    width: 330rpx;
    margin-bottom: 30rpx;
    //background: red;
    display: flex;
    flex-direction: column;
    align-items: center;
    border: 1rpx solid #f8f8f8;
    border-radius: 10rpx;
    padding: 20rpx;
    box-sizing: border-box;
    .text {
      text-align: left;
      margin-top: 20rpx;
      font-weight: 700;
      font-size: 24rpx;
    }
  }
}
.search {
  padding: 20rpx;
}
</style>
